/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jmenu.gui;

import java.awt.event.ActionEvent;
import javax.swing.JComboBox;
import jmenu.dbmodels.MFood;
import jmenu.dbmodels.MFoods;
import jmenu.main.Config;

/**
 *
 * @author sevcik.roman
 */
public class FoodComboBox extends JComboBox
{
    private MFoods foods;

    public FoodComboBox()
    {
        super();
        this.removeAllItems();
        this.addItem("Vyberte možnost");
        this.setEnabled(false);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        
        CategoryComboBox cmb =  (CategoryComboBox)e.getSource();
        int index = cmb.getSelectedIndex();
        switch(index)
        {
            case 0:
                this.removeAllItems();
                this.addItem("Vyberte možnost");
                this.setEnabled(false);
            break;
            default:
                foods = Config.getKernel().queries.loadAllFoodsBySectionID(index);
                if (foods == null)
                    break;
                this.removeAllItems();
                this.addItem("Vyberte možnost");

                for (int j = 0; j < foods.getRows().size(); j++)
                    this.addItem(foods.getRow(j).getName());
                this.setEnabled(true);
        }
        
    }

    public MFood getSelectedFood()
    {
        MFood food;
        if (this.getSelectedIndex() > 0)
        {
            food = this.foods.getRow(this.getSelectedIndex() - 1);
        }
        else
        {
            food = new MFood(0, 0, "Unused", 0, 0, 0, 0, 0, "", 0);
        }
        return food;
    }
}
